package team1.hdhnewsapp.mainactivity;

import info.androidhive.slidingmenu.R;

import java.text.DecimalFormat;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ExchangeRateActivity extends Activity {
	private static final String API_URL = "http://www.getexchangerates.com/api/latest.json";
	TextView ketqua;
	EditText editCost;
	String arr[] = { "VND", "GBP", "EUR", "USD" };
	Spinner spin, spin2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exchangerate);
		this.setTitle("Exchange Rates");
		editCost = (EditText) findViewById(R.id.edtCost);
		ketqua = (TextView) findViewById(R.id.tvKq);
		spin = (Spinner) findViewById(R.id.SpinnerFrom);
		spin2 = (Spinner) findViewById(R.id.SpinnerTo);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arr);
		adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spin.setAdapter(adapter);
		spin2.setAdapter(adapter);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent i = new Intent(this, MainActivity.class);
			startActivity(i);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	public void onClickConvert(View v) {
		AsyncHttpClient client = new AsyncHttpClient();
		client.get(API_URL, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(Throwable arg0, String arg1) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1);
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}

			@Override
			public void onSuccess(String response) {
				Log.i("CHACHING", "HTTP Sucess");

				try {
					JSONArray jsonArr = new JSONArray(response);
					Double from = Double.parseDouble(jsonArr.getJSONObject(0)
							.getString(spin.getSelectedItem().toString()));
					Log.i("Co 3", spin.getSelectedItem().toString());
					Double to = Double.parseDouble(jsonArr.getJSONObject(0)
							.getString(spin2.getSelectedItem().toString()));
					Double cost = Double.parseDouble(editCost.getText()
							.toString());
					Double ketquacuoi = (cost * to) / from;
					ketqua.setText("Kết quả = "
							+ new DecimalFormat("#,###,###.#")
									.format(ketquacuoi) + " "
							+ spin2.getSelectedItem().toString());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					Log.e("Error", "1");
					e.printStackTrace();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
